package ch.tiktak.games.powersudoku;

import au.com.bytecode.opencsv.CSVWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

public class CSVGridWriter {

    public static void write(GenericGrid grid, File file) throws IOException {
        CSVWriter writer = new CSVWriter(new FileWriter(file));
        writer.writeAll(convertFromIntegerList(grid.getData()));
        writer.checkError();
    }

    public static List<String[]> convertFromIntegerList(List<int[]> intList) {
        List<String[]> result = new LinkedList<String[]>();
        for(int i = 0; i < intList.size(); i++) {
            String[] subResult = new String[9];
            for(int j = 0; j < intList.get(i).length; j++) {
                subResult[j] = Integer.toString(intList.get(i)[j]);
            }
            result.add(subResult);
        }
        return result;
    }
}
